package com.lwlk.zdk.controller.Alarm;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwlk.zdk.enums.RetCode;
import com.lwlk.zdk.mapper.pw.pwAlarmRulesMapper;
import com.lwlk.zdk.mapper.pw.pwUserMapper;
import com.lwlk.zdk.model.CommonUser;
import com.lwlk.zdk.model.pw.pwAlarmRules;
import com.lwlk.zdk.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Api(value = "报警规则")
@RestController
@Transactional
@RequestMapping("/alarm/rules")
public class AlarmRulesController {

    private final static String port = PropUtils.getString("ports");
    private final static String Ip = PropUtils.getString("Ip");
    private final static String clientCode = PropUtils.getString("clientCode");

    @Autowired
    pwAlarmRulesMapper pwAlarmRulesMapper;
    @Autowired
    pwUserMapper pwUserMapper;



    @ApiOperation(value = "报警规则添加")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public JSONObject add(@Valid @RequestBody pwAlarmRules pwAlarmRules, HttpServletRequest request){
        boolean flag=listerUtil.getLister(request);
        if (flag==false){
            String list = "{\"code\":1009,\"msg\":\"此账号为监管员账号,不可进行操作\"}";
            return JSONObject.parseObject(list);
        }

        int i=pwAlarmRulesMapper.insertSelective(pwAlarmRules);
        Map map=new HashMap();
        map.put("ruleId",String.valueOf(pwAlarmRules.getId()));
        map.put("ruleType",String.valueOf(pwAlarmRules.getRuleType()));
        map.put("industryId",String.valueOf(pwAlarmRules.getIndustryId()));
        map.put("speedLimit",String.valueOf(pwAlarmRules.getSpeedLimit()));
        map.put("cotinueTime",String.valueOf(pwAlarmRules.getCotinueTime()));
        map.put("count",String.valueOf(pwAlarmRules.getCount()));
        map.put("startTime",pwAlarmRules.getStartTime());
        map.put("endTime",pwAlarmRules.getEndTime());
        map.put("downAlarmInfo",String.valueOf(pwAlarmRules.getDownAlarmInfo()));
        map.put("alarmContentChoice",pwAlarmRules.getAlarmContentChoice());
        map.put("alarmContentRemark",pwAlarmRules.getAlarmContentRemark());
        map.put("alarmContentPreview",pwAlarmRules.getAlarmContentPreview());
        map.put("stopContinueTime",String.valueOf(pwAlarmRules.getStopContinueTime()));
        map.put("oncePerVechilePerday",String.valueOf(pwAlarmRules.getOncePerVechilePerday()));
        map.put("voiceAlert",String.valueOf(pwAlarmRules.getVoiceAlert()));
        map.put("flashAlert",String.valueOf(pwAlarmRules.getFlashAlert()));
        String url="http://"+Ip+":"+port+"/dis/alarm_rule";
        Map<String,String>headMap=new HashMap<>();
        headMap.put("clientCode",clientCode);
        String result=HttpClientUtils.doPost(url,headMap,map);
        System.out.println(result);
        return JSONObject.parseObject(result);
    }


    //==========报警规则修改=================
/*    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Response update(pwAlarmRules pwAlarmRules){
        Response response=new Response();
        Map map=new HashMap();
        map.put("id",pwAlarmRules.getId());
        map.put("ruleType",pwAlarmRules.getRuleType());
        map.put("industryId",pwAlarmRules.getIndustryId());
        map.put("speedLimit",pwAlarmRules.getSpeedLimit());
        map.put("cotinueTime",pwAlarmRules.getCotinueTime());
        map.put("count",pwAlarmRules.getCount());
        map.put("startTime",pwAlarmRules.getStartTime());
        map.put("endTime",pwAlarmRules.getEndTime());
        map.put("downAlarmInfo",pwAlarmRules.getDownAlarmInfo());
        map.put("alarmContentChoice",pwAlarmRules.getAlarmContentChoice());
        map.put("alarmContentRemark",pwAlarmRules.getAlarmContentRemark());
        map.put("alarmContentPreview",pwAlarmRules.getAlarmContentPreview());
        map.put("stopContinueTime",pwAlarmRules.getStopContinueTime());
        map.put("oncePerVechilePerday",pwAlarmRules.getOncePerVechilePerday());
        map.put("voiceAlert",pwAlarmRules.getVoiceAlert());
        map.put("flashAlert",pwAlarmRules.getFlashAlert());
        String url="http://"+Ip+":"+port+"/dis/v1/alarm_rule";
        String result=HttpClientUtil.doPost(url,map);
        int i=pwAlarmRulesMapper.updateByExampleSelective(pwAlarmRules);
        response.setData(i);
        response.setCode(RetCode.SUCCESS);
        response.setMsg("执行成功");
        return response;
    }*/



     @ApiOperation(value = "报警规则展示")
    @RequestMapping(value = "/show",method = RequestMethod.GET)
    public LayuiTablePagingResult show(Integer page, Integer size){
        LayuiTablePagingResult result=null;
         PageHelper.startPage(page,size);
         List<pwAlarmRules> pwAlarmRules=pwAlarmRulesMapper.selectList();
         PageInfo pageInfo = new PageInfo(pwAlarmRules);
         result = new LayuiTablePagingResult();
         result.setCode(RetCode.SUCCESS.getCode());
         result.setMsg(RetCode.SUCCESS.getMsg());
         result.setCount(pageInfo.getTotal());
         result.setData(pageInfo.getList());
         Long totalPages = pageInfo.getTotal() / size;
         if (pageInfo.getTotal() % size != 0){
             totalPages ++;
         }
         result.setTotalPages(totalPages);
        return result;
    }
}

































